const MyPromise = require("../template.js")

/** 十、Promise.all 测试 */

// 测试 1: 所有 Promise 成功
function testAllSuccess() {
  const p1 = MyPromise.resolve(1)
  const p2 = MyPromise.resolve(2)
  const p3 = MyPromise.resolve(3)

  MyPromise.all([p1, p2, p3]).then((results) => {
    console.log("10.1 All success - length:", results.length === 3)
    console.log("10.2 All success - values:", results[0] === 1 && results[1] === 2 && results[2] === 3)
  })
}

// 测试 2: 包含普通值
function testAllWithNonPromise() {
  const p1 = MyPromise.resolve(1)
  const p2 = 2 // 普通值
  const p3 = MyPromise.resolve(3)

  MyPromise.all([p1, p2, p3]).then((results) => {
    console.log("10.3 All with non-promise:", results[0] === 1 && results[1] === 2 && results[2] === 3)
  })
}

// 测试 3: 有一个 Promise 失败（立即拒绝）
function testAllOneReject() {
  const p1 = MyPromise.resolve(1)
  const p2 = MyPromise.reject("Error")
  const p3 = MyPromise.resolve(3)

  MyPromise.all([p1, p2, p3]).then(
    (results) => console.log("10.4 All one reject:", false),
    (reason) => console.log("10.4 All one reject:", reason === "Error")
  )
}

// 测试 4: 空数组
function testAllEmpty() {
  MyPromise.all([]).then((results) => {
    console.log("10.5 All empty:", Array.isArray(results) && results.length === 0)
  })
}

// 测试 5: 异步 Promise（保持顺序）
function testAllAsyncOrder() {
  const p1 = new MyPromise((resolve) => setTimeout(() => resolve(1), 100))
  const p2 = new MyPromise((resolve) => setTimeout(() => resolve(2), 50))
  const p3 = new MyPromise((resolve) => setTimeout(() => resolve(3), 80))

  MyPromise.all([p1, p2, p3]).then((results) => {
    console.log("10.6 All async order:", results[0] === 1 && results[1] === 2 && results[2] === 3)
  })
}

// 测试 6: 第一个失败
function testAllFirstReject() {
  const p1 = MyPromise.reject("First Error")
  const p2 = MyPromise.resolve(2)
  const p3 = MyPromise.resolve(3)

  MyPromise.all([p1, p2, p3]).then(
    (results) => console.log("10.7 All first reject:", false),
    (reason) => console.log("10.7 All first reject:", reason === "First Error")
  )
}

// 测试 7: 最后一个失败
function testAllLastReject() {
  const p1 = MyPromise.resolve(1)
  const p2 = MyPromise.resolve(2)
  const p3 = MyPromise.reject("Last Error")

  MyPromise.all([p1, p2, p3]).then(
    (results) => console.log("10.8 All last reject:", false),
    (reason) => console.log("10.8 All last reject:", reason === "Last Error")
  )
}

// 测试 8: 异步失败（第一个失败的会被捕获）
function testAllAsyncReject() {
  const p1 = new MyPromise((resolve) => setTimeout(() => resolve(1), 100))
  const p2 = new MyPromise((_, reject) => setTimeout(() => reject("Fast Error"), 30))
  const p3 = new MyPromise((_, reject) => setTimeout(() => reject("Slow Error"), 150))

  MyPromise.all([p1, p2, p3]).then(
    (results) => console.log("10.9 All async reject:", false),
    (reason) => console.log("10.9 All async reject:", reason === "Fast Error")
  )
}

// 测试 9: 包含 thenable 对象
function testAllWithThenable() {
  const thenable = {
    then: (resolve) => {
      setTimeout(() => resolve(2), 20)
    }
  }
  const p1 = MyPromise.resolve(1)
  const p3 = MyPromise.resolve(3)

  MyPromise.all([p1, thenable, p3]).then((results) => {
    console.log("10.10 All with thenable:", results[0] === 1 && results[1] === 2 && results[2] === 3)
  })
}

// 测试 10: 链式调用
function testAllChain() {
  const p1 = MyPromise.resolve(1)
  const p2 = MyPromise.resolve(2)

  MyPromise.all([p1, p2])
    .then((results) => results.reduce((a, b) => a + b, 0))
    .then((sum) => console.log("10.11 All chain:", sum === 3))
}

testAllSuccess()
testAllWithNonPromise()
testAllOneReject()
testAllEmpty()
testAllAsyncOrder()
testAllFirstReject()
testAllLastReject()
testAllAsyncReject()
testAllWithThenable()
testAllChain()
